package com.ruoyi.information.service.pc.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.pc.SysNotice;
import com.ruoyi.information.mapper.pc.SysNoticeMapper;
import com.ruoyi.information.service.pc.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统通知Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-22
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * 查询系统通知
     *
     * @param id    系统通知主键
     * @param login
     * @return 系统通知
     */
    @Override
    public SysNotice selectSysNoticeById(Long id, boolean login) {
        if (login) {
            Integer size = sysNoticeMapper.selectSysNoticeByUser(SecurityUtils.getUserId(), id);
            if (size == null || size == 0) {
                /*添加已查看记录*/
                sysNoticeMapper.insertSysNoticeUser(id, SecurityUtils.getUserId());
            }
        }
        return sysNoticeMapper.selectSysNoticeById(id);
    }

    /**
     * 查询系统通知列表(未登录)
     *
     * @param sysNotice 系统通知
     * @return 系统通知
     */
    @Override
    public List<SysNotice> selectSysNoticeList(SysNotice sysNotice) {
        return sysNoticeMapper.selectSysNoticeList(sysNotice);
    }

    /**
     * 系统通知列表（登录）
     */
    @Override
    public List<SysNotice> selectSysNoticeLoginList(SysNotice sysNotice) {
        List<SysNotice> notices = new ArrayList<>();
        sysNotice.setNoticeType(0L);
        List<SysNotice> notices2 = sysNoticeMapper.selectSysNoticeList(sysNotice);
        if (notices2 != null && !notices2.isEmpty()) {
            notices.addAll(notices2);
        }
        sysNotice.setNoticeType(1L);
        sysNotice.setSendUserId(String.valueOf(SecurityUtils.getUserId()));
        List<SysNotice> notices3 = sysNoticeMapper.selectSysNoticeList(sysNotice);
        if (notices3 != null && !notices3.isEmpty()) {
            notices.addAll(notices3);
        }
        sysNotice.setNoticeType(2L);
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if (roles != null && !roles.isEmpty()) {
            for (String role : roles) {
                sysNotice.setSendUserId(role);
                List<SysNotice> notices4 = sysNoticeMapper.selectSysNoticeList(sysNotice);
                if (notices4 != null && !notices4.isEmpty()) {
                    notices.addAll(notices4);
                }
            }
        }
        for (SysNotice notice : notices) {
            notice.setIsLook(sysNoticeMapper.selectSysNoticeByUser(SecurityUtils.getUserId(), notice.getId()));
        }
        return notices;
    }

    /**
     * 新增系统通知
     *
     * @param sysNotice 系统通知
     * @return 结果
     */
    @Override
    public int insertSysNotice(SysNotice sysNotice) {
        sysNotice.setCreateTime(DateUtils.getNowDate());
        return sysNoticeMapper.insertSysNotice(sysNotice);
    }

    /**
     * 修改系统通知
     *
     * @param sysNotice 系统通知
     * @return 结果
     */
    @Override
    public int updateSysNotice(SysNotice sysNotice) {
        sysNotice.setUpdateTime(DateUtils.getNowDate());
        return sysNoticeMapper.updateSysNotice(sysNotice);
    }

    /**
     * 批量删除系统通知
     *
     * @param ids 需要删除的系统通知主键
     * @return 结果
     */
    @Override
    public int deleteSysNoticeByIds(Long[] ids) {
        return sysNoticeMapper.deleteSysNoticeByIds(ids);
    }

    /**
     * 删除系统通知信息
     *
     * @param id 系统通知主键
     * @return 结果
     */
    @Override
    public int deleteSysNoticeById(Long id) {
        return sysNoticeMapper.deleteSysNoticeById(id);
    }
}
